
package com.epam.android.training.app.db;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Class to work with DB. Provide inserting, deleting, operation. Also provide
 * method to return all feeds/feeds items.
 * 
 * @author Natalie
 */
public class FeedsDb {

  private FeedsOpenHelper helper;

  public FeedsDb(Context context) {
    helper = new FeedsOpenHelper(context);

  }

  public boolean insertFeed(String title, String url) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(FeedsOpenHelper.TITLE_FIELD, title);
    values.put(FeedsOpenHelper.URL_FIELD, url);
    return (db.insert(FeedsOpenHelper.FEEDS_TABLE_NAME, null, values) > 0);

  }

  public boolean insertFeedItem(String title, long date, String url) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(FeedsOpenHelper.TITLE_FIELD, title);
    values.put(FeedsOpenHelper.DATE_FIELD, date);
    values.put(FeedsOpenHelper.URL_FIELD, url);
    return (db.insert(FeedsOpenHelper.FEEDS_ITEMS_TABLE_NAME, null, values) > 0);

  }

  public boolean removeFeed(Long feed_id) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("feed_id", feed_id);
    return (db.delete(FeedsOpenHelper.FEEDS_TABLE_NAME, "feed_id=" + feed_id.toString(), null) > 0);

  }

  public boolean removeFeedItem(Long feed_item_id) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(FeedsOpenHelper.ITEMID_FIELD, feed_item_id);
    return (db.delete(FeedsOpenHelper.FEEDS_ITEMS_TABLE_NAME, FeedsOpenHelper.ITEMID_FIELD + feed_item_id.toString(),
        null) > 0);

  }

  public Cursor getFeedsCursor() {
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor cursor = db.query(FeedsOpenHelper.FEEDS_TABLE_NAME, null, null, null, null, null,
        FeedsOpenHelper.TITLE_FIELD);
    return cursor;
  }

  public Cursor getFeedItemsCursor() {
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor cursor = db.query(FeedsOpenHelper.FEEDS_ITEMS_TABLE_NAME, null, null, null, null, null,
        FeedsOpenHelper.DATE_FIELD + " DESC");
    return cursor;
  }

  /**
   * Actually since API level 8 function insertWithOnConflict exist, but I'm
   * supporting API with lower level, so
   * 
   * @param title
   * @param date
   * @param url
   * @return
   * @throws MalformedURLException
   */
  public boolean insertIfNotExist(String title, long date, String url) {
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor cursor = db.query(FeedsOpenHelper.FEEDS_ITEMS_TABLE_NAME, new String[] {
      FeedsOpenHelper.URL_FIELD
    }, FeedsOpenHelper.URL_FIELD + "=?", new String[] {
      url
    }, null, null, FeedsOpenHelper.DATE_FIELD + " DESC");

    boolean a = false;
    if (null != cursor && cursor.getCount() > 0) {
      a = false;
    } else {
      a = insertFeedItem(title, date, url);
    }
    cursor.close();
    return a;
  }

  public void close() {
    helper.close();
  }

  /**
   * Return Feeds
   * 
   * @return
   * @throws MalformedURLException
   */
  public List<Feed> getFeeds() throws MalformedURLException {
    List<Feed> list = new ArrayList<Feed>();
    Cursor c = getFeedsCursor();
    c.moveToFirst();
    while (c.isAfterLast() == false) {
      Feed f = new Feed(c.getLong(0), c.getString(1), new URL(c.getString(2)));
      list.add(f);
      c.moveToNext();
    }
    c.close();
    return list;

  }

  /**
   * Return feed Items
   * 
   * @return
   * @throws MalformedURLException
   */
  public List<FeedItem> getFeedItems() throws MalformedURLException {
    List<FeedItem> list = new ArrayList<FeedItem>();
    Cursor c = getFeedItemsCursor();
    c.moveToFirst();
    while (c.isAfterLast() == false) {
      Date date = new Date(c.getLong(2));

      FeedItem f = new FeedItem(c.getLong(0), c.getString(1), new URL(c.getString(3)), date.toGMTString());
      list.add(f);
      c.moveToNext();
    }
    c.close();
    return list;
  }
}
